package com.cn.wanxi.servlet.back.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.DataEncoding;
import com.cn.wanxi.util.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/back/product/info")
public class ProductInfoServlet extends HttpServlet {
    IProductService productService = new ProductServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setTextContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        //产品详情
        String pid = req.getParameter("id");
        int id = 0;
        if (!"".equals(pid))
            id = Integer.parseInt(pid);
        ProductModel productModel = productService.getProduct(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("product",productModel);
        resp.getWriter().print(jsonObject);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setJsonContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        //产品分页列表和模糊查询
        String name = req.getParameter("name");
        String minPrice = req.getParameter("min_price");
        String maxPrice = req.getParameter("max_price");
        String product_type = req.getParameter("product_type");
        String user_type = req.getParameter("user_type");
        String pageSize = req.getParameter("page");
        String pageNum = req.getParameter("size");
        String sort = req.getParameter("sort");
        int page = Integer.parseInt(pageSize);
        int size = Integer.parseInt(pageNum);
        BigDecimal min_price;
        BigDecimal max_price;
        try {
            min_price = new BigDecimal(minPrice);
        }catch (Exception e){
            min_price = new BigDecimal("0.0");
        }
        try {
            max_price = new BigDecimal(maxPrice);
        }catch (Exception e){
            max_price = new BigDecimal("0.0");
        }
        Map<String ,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("min_price",min_price);
        map.put("max_price",max_price);
        map.put("product_type",product_type);
        map.put("user_type",user_type);
        map.put("page",(page-1)*size);
        map.put("size",size);
        map.put("sort",sort);
        PageModel<ProductModel> productList = productService.getProducts(map);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("products",productList);
        resp.getWriter().print(jsonObject);
    }
}
